enum MessageType {
  text,
  image,
  video,
}

class ChatMessage {
  final String content;
  final bool isUser;
  final DateTime timestamp;
  final MessageType type;
  final String? mediaPath;

  ChatMessage({
    required this.content,
    required this.isUser,
    required this.timestamp,
    this.type = MessageType.text,
    this.mediaPath,
  });

  Map<String, dynamic> toJson() => {
    'content': content,
    'isUser': isUser,
    'timestamp': timestamp.toIso8601String(),
    'type': type.toString(),
    'mediaPath': mediaPath,
  };

  factory ChatMessage.fromJson(Map<String, dynamic> json) => ChatMessage(
    content: json['content'],
    isUser: json['isUser'],
    timestamp: DateTime.parse(json['timestamp']),
    type: MessageType.values.firstWhere(
      (e) => e.toString() == json['type'],
      orElse: () => MessageType.text,
    ),
    mediaPath: json['mediaPath'],
  );

  // 创建文本消息
  static ChatMessage text({
    required String content,
    required bool isUser,
  }) {
    return ChatMessage(
      content: content,
      isUser: isUser,
      timestamp: DateTime.now(),
      type: MessageType.text,
    );
  }

  // 创建图片消息
  static ChatMessage image({
    required String content,
    required bool isUser,
    required String imagePath,
  }) {
    return ChatMessage(
      content: content,
      isUser: isUser,
      timestamp: DateTime.now(),
      type: MessageType.image,
      mediaPath: imagePath,
    );
  }

  // 创建视频消息
  static ChatMessage video({
    required String content,
    required bool isUser,
    required String videoPath,
  }) {
    return ChatMessage(
      content: content,
      isUser: isUser,
      timestamp: DateTime.now(),
      type: MessageType.video,
      mediaPath: videoPath,
    );
  }
} 